package com.lsp.rocketmqprovider.controller;

import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@RestController
public class SendMessageController {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    /**
     * 顺序生产
     * @return
     */
    @RequestMapping("/send/orderly")
    public String sendOrderlyMsg(){
        //异步发送
        rocketMQTemplate.asyncSendOrderly("orderlyTopic", "存钱1000", "order", new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                System.out.println("异步消息发送成功！");
            }
            @Override
            public void onException(Throwable throwable) {
                System.out.printf("异步消息发送失败！",throwable);
            }
        });
        //同步发送
        rocketMQTemplate.syncSendOrderly("orderlyTopic","存钱1000","order");
        rocketMQTemplate.syncSendOrderly("orderlyTopic","取钱1000","order");
        rocketMQTemplate.syncSendOrderly("orderlyTopic","存钱1000","order");
        rocketMQTemplate.syncSendOrderly("orderlyTopic","存钱1000","order");
        rocketMQTemplate.syncSendOrderly("orderlyTopic","存钱1000","order");
        rocketMQTemplate.syncSendOrderly("orderlyTopic","取钱1000","order");
        rocketMQTemplate.syncSendOrderly("orderlyTopic","取钱1000","order");
        rocketMQTemplate.syncSendOrderly("orderlyTopic","取钱1000","order");
        rocketMQTemplate.syncSendOrderly("orderlyTopic","存钱1000","order");
        return "success";
    }

    /**
     * 延迟生产
     * @return
     */
    @RequestMapping("/send/delay")
    public String sendDelayMsg(){
        rocketMQTemplate.syncSend("delayTopic", MessageBuilder.withPayload("发送时间："+ LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ssSSS")) +"rocketmq延迟1秒发送消息").build(),3000,1);
        rocketMQTemplate.syncSend("delayTopic", MessageBuilder.withPayload("发送时间："+ LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ssSSS")) +"rocketmq延迟5秒发送消息").build(),3000,2);
        rocketMQTemplate.syncSend("delayTopic", MessageBuilder.withPayload("发送时间："+ LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ssSSS")) +"rocketmq延迟10秒发送消息").build(),3000,3);
        return "success";
    }

    /**
     * 批量生产
     * @return
     */
    @RequestMapping("/send/batch")
    public String sendBatchMsg() throws UnsupportedEncodingException {
        List<Message> messages = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            String content = "Hello batch message " + i;
            Message message = new Message("TopicTest", content.getBytes(RemotingHelper.DEFAULT_CHARSET));

            messages.add(message);
        }
        rocketMQTemplate.syncSend("batchTopic", messages,3000);
        return "success";
    }


    /**
     * 事务生产
     * @return
     */
    @RequestMapping("/send/transaction")
    public String sendTransactionMsg() {
        rocketMQTemplate.sendMessageInTransaction("transactionTopic",MessageBuilder.withPayload("----------事务消息----------").build(),null);
        return "success";
    }

}
